using MediatR;
using Warehouse.API.Read.App.Commands;
using Warehouse.Dto;
using Warehouse.Repository;

namespace Warehouse.API.Read.App.Handlers
{
    /// <summary>
    /// 调拨单列表查询处理器
    /// </summary>
    public class AllocationListQueryHandler : IRequestHandler<AllocationListQuery, AllocationQueryResponse>
    {
        private readonly IAllocationRepository _allocationRepository;

        public AllocationListQueryHandler(IAllocationRepository allocationRepository)
        {
            _allocationRepository = allocationRepository;
        }

        public async Task<AllocationQueryResponse> Handle(AllocationListQuery request, CancellationToken cancellationToken)
        {
            var (data, totalCount) = await _allocationRepository.GetPagedListAsync(
                request.PageIndex,
                request.PageSize,
                request.TransferNo,
                request.FromWarehouseId,
                request.ToWarehouseId,
                request.BillStatus,
                request.OutStatus,
                request.InStatus,
                request.BeginDate,
                request.EndDate);

            var totalPage = (int)Math.Ceiling((double)totalCount / request.PageSize);

            var items = data.Select(x => new AllocationQueryItemDto
            {
                TransferId = x.Id,
                TransferNo = x.TransferNo,
                ApplyDate = x.ApplyDate,
                TransferType = x.TransferType,
                FromWarehouseId = x.FromWarehouseId,
                FromWarehouseName = GetWarehouseName(x.FromWarehouseId),
                ToWarehouseId = x.ToWarehouseId,
                ToWarehouseName = GetWarehouseName(x.ToWarehouseId),
                BillStatus = x.BillStatus,
                BillStatusName = GetBillStatusName(x.BillStatus),
                OutStatus = x.OutStatus,
                OutStatusName = GetOutStatusName(x.OutStatus),
                InStatus = x.InStatus,
                InStatusName = GetInStatusName(x.InStatus),
                TotalQty = x.TotalQty,
                TotalAmount = x.TotalAmount,
                HandlerName = x.HandlerName ?? "",
                ApplicantName = x.ApplicantName ?? "",
                CreatedAt = x.CreatedAt,
                UpdatedAt = x.UpdatedAt
            }).ToList();

            return new AllocationQueryResponse
            {
                TotalCount = totalCount,
                TotalPage = totalPage,
                Data = items
            };
        }

        private static string GetWarehouseName(long warehouseId)
        {
            // 这里应该从数据库查询仓库名称，暂时返回模拟数据
            return warehouseId switch
            {
                1 => "成品仓库一",
                2 => "成品仓库二",
                3 => "原料仓库一",
                4 => "原料仓库二",
                _ => $"仓库{warehouseId}"
            };
        }

        private static string GetBillStatusName(byte billStatus)
        {
            return billStatus switch
            {
                10 => "待审核",
                20 => "待审核",
                30 => "待出库",
                60 => "待入库",
                100 => "已完成",
                90 => "驳回",
                _ => "未知状态"
            };
        }

        private static string GetOutStatusName(byte outStatus)
        {
            return outStatus switch
            {
                0 => "未出库",
                1 => "部分出库",
                2 => "已出库",
                _ => "未知状态"
            };
        }

        private static string GetInStatusName(byte inStatus)
        {
            return inStatus switch
            {
                0 => "未入库",
                1 => "部分入库",
                2 => "已入库",
                _ => "未知状态"
            };
        }
    }
}

